using System;
using System.Collections;
using System.Management;

namespace Niu
{
    public enum nLogicalDiskType
    {
        Unknown,
        NoRootDirectory,
        RemovableDisk,
        LocalDisk,
        NetworkDrive,
        CompactDisc,
        RAMDisk,
    }

    public class nLogicalDisk
    {
        private ManagementObject mo = null;

        internal nLogicalDisk(ManagementObject mo)
        {
            this.mo = mo;
        }

        public String Name
        {
            get
            {
                try
                {
                    return mo["Name"].ToString();
                }
                catch
                {
                    return "";
                }
            }
         }

        public String Caption
        {
            get
            {
                try
                {
                    return mo["Caption"].ToString();
                }
                catch
                {
                    return "";
                }
            }
        }

        public String Description
        {
            get
            {
                try
                {
                    return mo["Description"].ToString();
                }
                catch
                {
                    return "";
                }
            }
        }

        public String VolumeName
        {
            get
            {
                try
                {
                    return mo["VolumeName"].ToString();
                }
                catch
                {
                    return "";
                }
            }
        }

        public String FileSystem
        {
            get
            {
                try
                {
                    return mo["FileSystem"].ToString();
                }
                catch
                {
                    return "";
                }
            }
        }

        public UInt64 Size
        {
            get
            {
                try
                {
                    return Convert.ToUInt64(mo["Size"]);
                }
                catch
                {
                    return 0;
                }
            }
        }

        public UInt64 FreeSpace
        {
            get
            {
                try
                {
                    return Convert.ToUInt64(mo["FreeSpace"]);
                }
                catch
                {
                    return 0;
                }
            }
        }

        public nLogicalDiskType DriveType
        {
            get
            {
                try
                {
                    return (nLogicalDiskType)(Convert.ToUInt32(mo["DriveType"]));
                }
                catch
                {
                    return nLogicalDiskType.Unknown;
                }
            }
        }
    }

    public static class nLogicalDisks
    {
        public static nLogicalDisk[] GetAll()
        {
            ManagementClass mc = new ManagementClass("Win32_LogicalDisk");
            ManagementObjectCollection moc = mc.GetInstances();
            ArrayList items = new ArrayList();

            foreach (ManagementObject mo in moc)
            {
                items.Add(new nLogicalDisk(mo));
            }

            return (nLogicalDisk[]) items.ToArray(typeof(nLogicalDisk));
        }
    }
}
